<?php
/**
 * Created by PhpStorm.
 * User: jwb
 * Date: 2021/4/29
 * Time: 14:23
 */

namespace App\Extensions\Actions;

use App\Models\AdminUser;
use Encore\Admin\Actions\RowAction;

class BaseRowAction extends RowAction
{
    /**
     * @param \Encore\Admin\Grid\Displayers\Actions $actions
     * @param null $name
     */
    public function setActions(\Encore\Admin\Grid\Displayers\Actions $actions, $name)
    {
        $this->setGrid($actions->getGrid())->setRow($actions->row);
        $this->name = $name;
        return $this;
    }

    public function authorize(AdminUser $user, $model)
    {
        return $user->role_type == 1 || $model->user_id == $user->id;
    }

    public function render()
    {
        if ($href = $this->href()) {
            return "<a href='{$href}'>{$this->name()}</a>";
        }

        $this->addScript();

        $attributes = $this->formatAttributes();

        return sprintf(
            "<a data-_key='%s' href='javascript:void(0);' class='%s' {$attributes}>%s</a>",
            $this->getKey(),
            $this->getElementClass(),
            $this->name()
        );
    }

    public function __toString()
    {
        return $this->render();
    }

}
